using System;
using System.Collections.Generic;
using System.Linq;

namespace TXK
{
    /// <summary>
    /// Short转换类
    /// </summary>
    public class ShortLib
    {
        /// <summary>
        /// 字节数组中截取转成16位整型
        /// </summary>
        /// <param name="source"></param>
        /// <param name="start"></param>
        /// <param name="type"></param>
        /// <returns></returns>
        public static short GetShortFromByteArray(byte[] source, int start = 0, DataFormat type = DataFormat.ABCD)
        {
            byte[] array = ByteArrayLib.Get2ByteArray(source, start, type);
            return (short)((array != null) ? BitConverter.ToInt16(array, 0) : 0);
        }

        /// <summary>
        /// 将字节数组中截取转成16位整型数组
        /// </summary>
        /// <param name="source"></param>
        /// <param name="type"></param>
        /// <returns></returns>
        public static short[] GetShortArrayFromByteArray(byte[] source, DataFormat type = DataFormat.ABCD)
        {
            short[] array = new short[source.Length / 2];
            for (int i = 0; i < array.Length; i++)
            {
                array[i] = GetShortFromByteArray(source, i * 2, type);
            }
            return array;
        }

        /// <summary>
        /// 将字符串转转成16位整型数组
        /// </summary>
        /// <param name="val"></param>
        /// <param name="spilt"></param>
        /// <returns></returns>
        public static short[] GetShortArrayFromString(string val, char spilt = ' ')
        {
            val = val.Trim();
            List<short> list = new List<short>();
            if (val.Contains(spilt))
            {
                string[] array = val.Split(new char[1] { spilt }, StringSplitOptions.RemoveEmptyEntries);
                string[] array2 = array;
                foreach (string text in array2)
                {
                    list.Add(Convert.ToInt16(text.Trim()));
                }
            }
            else
            {
                list.Add(Convert.ToInt16(val.Trim()));
            }
            return list.ToArray();
        }

        /// <summary>
        /// 设置16位整型某个位
        /// </summary>
        /// <param name="value"></param>
        /// <param name="bit"></param>
        /// <param name="val"></param>
        /// <param name="dataFormat"></param>
        /// <returns></returns>
        public static short SetbitValueFromShort(short value, int bit, bool val, DataFormat dataFormat = DataFormat.ABCD)
        {
            byte[] byteArrayFromShort = ByteArrayLib.GetByteArrayFromShort(value, dataFormat);
            if (bit >= 0 && bit <= 7)
            {
                byteArrayFromShort[1] = ByteLib.SetbitValue(byteArrayFromShort[1], bit, val);
            }
            else
            {
                byteArrayFromShort[0] = ByteLib.SetbitValue(byteArrayFromShort[0], bit - 8, val);
            }
            return GetShortFromByteArray(byteArrayFromShort, 0, dataFormat);
        }

        /// <summary>
        /// 设置字节数组某个位
        /// </summary>
        /// <param name="bt"></param>
        /// <param name="bit"></param>
        /// <param name="val"></param>
        /// <param name="dataFormat"></param>
        /// <returns></returns>
        public static short SetbitValueFrom2ByteArray(byte[] bt, int bit, bool val, DataFormat dataFormat = DataFormat.ABCD)
        {
            if (bit >= 0 && bit <= 7)
            {
                bt[1] = ByteLib.SetbitValue(bt[1], bit, val);
            }
            else
            {
                bt[0] = ByteLib.SetbitValue(bt[0], bit - 8, val);
            }
            return GetShortFromByteArray(bt, 0, dataFormat);
        }
    }
}
